package com.tianquan.server.handler;


import com.tianquan.common.message.LoginRequestMessage;
import com.tianquan.common.message.LoginResponseMessage;
import com.tianquan.server.config.ServerConfig;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;



/**
 * @author 天权
 */
@Slf4j
@ChannelHandler.Sharable
public class LoginRequestMessageHandler extends SimpleChannelInboundHandler<LoginRequestMessage> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, LoginRequestMessage msg) throws Exception {
        log.info("--------------------收到登录请求----------------- {}", msg);
        String username = msg.getUsername();
        String password = msg.getPassword();
        LoginResponseMessage message;
        if (ServerConfig.login(username, password)) {
            log.info("用户 {} 登录成功", username);
            message = new LoginResponseMessage(true, "登录成功");
            ctx.writeAndFlush(message);
        } else {
            log.info("用户 {} 登录失败", username);
            message = new LoginResponseMessage(false, "用户名或密码不正确");
            ctx.writeAndFlush(message);
            ctx.close();
        }
    }
}
